package com.server.questionbank.snframe.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.server.questionbank.snframe.domain.Course.Result.CourseFigures;
import com.server.questionbank.snframe.domain.Course.TCourse;
import com.server.questionbank.snframe.domain.User.TUser;

import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface TUserMapper extends BaseMapper<TUser> {
    // 获取所有用户信息
    @Select("SELECT * FROM T_user")
    public List<TUser> selectAllUser();

    // 通过手机号查询用户
    @Select("SELECT * FROM T_user WHERE phone_number = #{phoneNumber}")
    public TUser selectUserByPhoneNumber(@Param("phoneNumber") String phoneNumber);
    // 通过手机号和密码查询用户
    @Select("SELECT user_id FROM T_user WHERE phone_number = #{phoneNumber} AND password_hash = #{password}")
    public Long selectUserByPhoneNumberAndPassword(@Param("phoneNumber") String phoneNumber, @Param("password") String password);

    // 获取用户信息
    @Select("SELECT * FROM T_user WHERE user_id = #{userId}")
    public TUser selectUserById(@Param("userId") Long userId);

    // 添加用户
    @Insert("INSERT INTO T_user " +
            "(phone_number, password_hash, nick_name, resume, supermanager, update_by) VALUES " +
            "(#{user.phoneNumber}, #{user.passwordHash}, #{user.nickName}, '这个人很神秘，什么都没有写~~~', 'N','system');")
    @Options(useGeneratedKeys = true, keyProperty = "userId")  // 指定返回生成的主键，并将其设置到 TCourse 对象的 courseId 属性
    public int insertUser(@Param("user") TUser user);

    // 修改头像
    @Update("UPDATE T_user SET avatar = #{avatar} WHERE user_id = #{userId}")
    public int updateUserAvatar(@Param("userId") Long userId, @Param("avatar") String avatar);

    // 获取学生数量
    @Select("SELECT COUNT(DISTINCT user_regist_cus.user_id) " +
            "FROM T_course " +
            "JOIN user_regist_cus ON T_course.course_id = user_regist_cus.course_id " +
            "WHERE T_course.create_by = 2")
    public Long getStudentCount(Long userId);

    // 获取课程数量
    @Select("SELECT COUNT(*) FROM T_course WHERE create_by = #{userId}")
    public Long getCourseCount(Long userId);

    // 获取创建的课程
    @Select("SELECT * FROM v_course_full_info WHERE create_by = #{userId}")
    public List<CourseFigures> getCreateCourse(Long userId);

    // 获取加入的课程
    @Select("SELECT * FROM v_course_full_info " +
            "JOIN user_regist_cus ON v_course_full_info.course_id = user_regist_cus.course_id " +
            "WHERE user_regist_cus.user_id = #{userId}")
    public List<CourseFigures> getJoinCourse(Long userId);

    // 获取收藏的课程
    @Select("SELECT * FROM v_course_full_info " +
            "JOIN T_collection collection ON v_course_full_info.course_id = collection.traget_id " +
            "WHERE collection.type = 'course' AND collection.user_id = #{userId}")
    public List<CourseFigures> getCollectCourse(Long userId);
}